package com.dianshang.springdataredis;

import com.dianshang.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 * String类型
 */
@RunWith(SpringJUnit4ClassRunner.class) // Spring整合Redis的核心类
@ContextConfiguration(locations = "classpath:applicationContext-redis.xml") // 上下文配置文件
public class StringDemo {

    @Autowired
    private RedisTemplate redisTemplate;

    // 添加
    @Test
    public void add(){
        User user1 = new User(1,"张三","123");
        User user2 = new User(2,"李四","456");
        User user3 = new User(3,"王五","789");
        redisTemplate.boundValueOps("user:"+user1.getId()).set(user1);
        redisTemplate.boundValueOps("user:"+user2.getId()).set(user2);
        redisTemplate.boundValueOps("user:"+user3.getId()).set(user3);
    }

    // 根据条件查询
    @Test
    public void find(){
        Integer id1 = 1;
        Integer id2 = 2;
        Integer id3 = 3;
        User user1 =(User) redisTemplate.boundValueOps("user:"+id1).get();
        User user2 =(User) redisTemplate.boundValueOps("user:"+id2).get();
        User user3 =(User) redisTemplate.boundValueOps("user:"+id3).get();
        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user3);
        // 查询最后一条数据
        // User user =(User) redisTemplate.boundValueOps("user").get();
        // System.out.println(user);
    }

    // 根据条件删除
    @Test
    public void delete(){
        Integer id = 1;
        redisTemplate.delete("user:"+id);
        // 物理删除所有
        // redisTemplate.delete("user");
    }

    // 自增
    @Test
    public void incr(){
        // 默认为5——>每次启动就+默认值
        redisTemplate.boundValueOps("user").increment(5);
    }

}